package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.SpecimenDocument;
import com.fsnbigdata.domain.QSpecimenDocument;
import com.fsnbigdata.domain.Specimen;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = SpecimenDocumentRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = SpecimenDocumentRepositoryCustom.class)
@Transactional(readOnly = true)
public class SpecimenDocumentRepositoryImpl extends QueryDslRepositorySupportExt<SpecimenDocument> implements SpecimenDocumentRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    SpecimenDocumentRepositoryImpl() {
        super(SpecimenDocument.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String AUTHOR = "author";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SPECIMEN = "specimen";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    @Override
    public Page<SpecimenDocument> findAll(Predicate predicate, Pageable pageable) {
        QSpecimenDocument specimenDocument = QSpecimenDocument.specimenDocument;
        JPQLQuery<SpecimenDocument> query = from(specimenDocument).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable, specimenDocument);
    }
    /**
     * TODO Auto-generated method documentation
     *
     * @param specimen
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<SpecimenDocument> findBySpecimen(Specimen specimen, GlobalSearch globalSearch, Pageable pageable) {
        QSpecimenDocument specimenDocument = QSpecimenDocument.specimenDocument;
        JPQLQuery<SpecimenDocument> query = from(specimenDocument);
        Assert.notNull(specimen, "specimen is required");
        query.where(specimenDocument.specimen.eq(specimen));
        Path<?>[] paths = new Path<?>[] { specimenDocument.specimen, specimenDocument.name, specimenDocument.author };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(SPECIMEN, specimenDocument.specimen).map(NAME, specimenDocument.name).map(AUTHOR, specimenDocument.author);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, specimenDocument);
    }
}
